/*************************************************
 *FileName:      myprop.js
 *Description:   我的道具.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: true           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var page = 1,page_num=6;
    var isUp = true;
    /*
     * 主程序
     */ 
    window.addEventListener('refreshmyprop', function(e){//执行刷新
        location.reload(); 
    });
    
    $(document).ready(function() 
    {
        pagecss();
        //获取碎片
//       getchip();
        //获取商品
        getshop('');
        //限时道具
//      proptime();
//      //获取卡片
//      getcard();
//      //获取礼物
//      getgift();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        mui.plusReady(getchip)
        mui.plusReady(proptime)
        mui.plusReady(getcard)
        mui.plusReady(getgift)
        $('.myprop').on('tap','.btn',function(){
            var oname =$(this).siblings('.prop-name').text();
            var id = $(this).parents('.prop').attr('data-id');
            mui.confirm('确定兑换'+oname+'？','提示',['确定','取消'],function(e){
                if(e.index=='0'){
                    buyclick(id);
                }
            },'div');
        })
        
        
        $('.myprop').on('tap','a.con-gift-a',function(){
            if($(this).parent().nextAll().is(':hidden')){
                $(this).removeClass('mui-icon-arrowdown').addClass('mui-icon-arrowup') ;
                $(this).parent().nextAll().show();
            }else{
                $(this).removeClass('mui-icon-arrowup').addClass('mui-icon-arrowdown');
                $(this).parent().nextAll().hide();
            }
        })//礼物下拉框
        
        $('.myprop').on('tap','.con-title a',function(){
            $(this).addClass('active').siblings().removeClass('active');
            $('.mui-scroll-wrapper').eq($(this).index()).addClass('active').siblings('.mui-scroll-wrapper').removeClass('active');
        })
        
        $('.search').on('input',function(){
            var oval =$(this).val();
            if(oval != ''){
                $('.main').css({display:'none'})
                $('.searchshop').css({display:'block'})
                page=1;
                isUp = true;
                getshop(oval);
            }else{
                $('.main').css({display:'block'})
                $('.searchshop').css({display:'none'})
            }
            
        })
    }


//获取碎片
function getchip(){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_gift/getUsersPieces",
        dataType:'jsonp',
        data: {uid:uid,},
        success: function(data) {
            if(data.error_code == 0){
                var ochip='';
                if(!data.data.length){
                    $('.con').remove();
                }else if($('.con').length<1){
                    $('.con-action').before('<div class="con"></div>')
                }
                for(var i=0 ; i<data.data.length ; i++){
                    ochip +='<div class="chip"><p><img src="'+DOMAIN_NAME+data.data[i].url+'" /></p><p class="chip-sub"> '+data.data[i].name+'X'+data.data[i].sum+'</p><p class="chip-money">'+parseInt(data.data[i].money)+'块换币</p></div>';
                }
                $('.mui-scroll-wrapper').eq(0).find('.con').html(ochip)
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
                $('.mui-scroll-wrapper').eq(0).find('.con').html('<p class="nochip">暂无任何碎片</p>');
            }
        }  
    });
}

//获取商品
function getshop(val){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/good/getGoodsList",
        dataType:'jsonp',
        data: {
            uid:uid,
            page:page,
            page_num:page_num,
            words:val
        },
        success: function(data) {
            if(data.error_code == 0){
                var shop='';
                if(data.data.length == page_num){
                    $('.mui-scroll').children('.ys-scroll-btm').html('上拉加载更多');
                    isUp = true;
                }else{
                    $('.mui-scroll').children('.ys-scroll-btm').html('已经到底了');
                    isUp = false;
                }
                $('.mui-scroll').children('.ys-scroll-top').html('下拉刷新数据').attr('style', false);
                for(var i=0 ; i<data.data.length ; i++){
                    shop +='<div class="prop" data-id="'+data.data[i].id+'"><div class="prop-img"><img src="'+DOMAIN_NAME+data.data[i].url+'" /></div>'
                        +'<p class="prop-name">'+data.data[i].name+'</p><p class="prop-nub">还剩'+data.data[i].stock+'件</p>'
                        +'<p class="prop-nub"><span class="color">'+data.data[i].pieces_num+'</span>'+data.data[i].pieces_name+'碎片</p><a class="btn">立即兑换</a></div>'
                }
                if(!val){
                   $('.mui-scroll-wrapper').eq(0).find('.main').append(shop);
                }else{
                    page >1&&isUp==true ?$('.searchshop').append(shop):$('.searchshop').html(shop);
                }
                page++;
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
                $('.searchshop').html('')
                $('.mui-scroll').children('.ys-scroll-btm').html('已经到底了');
                isUp = false;
            }
        }  
    });
}

//判断碎片数量
function buyclick(id){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_gift/piecesNumCheck",
        dataType:'jsonp',
        data: {uid:uid,token:token,id:id},
        success: function(data) {
            if(data.error_code == 0){
                buyclick1(id);
            }else if(data.error_code == 1001){
                setTimeout(function(){
                    exitlogin();
                    location.href("login.html");
                },1000);
            }else if(data.error_code == 1){
                mui.confirm('您的'+data.error_msg+'，还需支付<br /><span class="color">'+data.data+'</span>换币，是否支付？','提示',['支付','取消'],function(e){if(e.index==0){buyclick1(id)}},'div');
            }
        }  
    });
}

//兑换
function buyclick1(id){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/order/orderAdd",
        dataType:'jsonp',
        data: {uid:uid,token:token,id:id},
        success: function(data) {
            if(data.error_code == 0){
                mui.toast('兑换成功');
                getchip();
                setTimeout(function(){common.pageTransition('site.html?orderid='+data.order_id+'')},1000)
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
                mui.toast(data.error_msg);
            }
        }  
    });
}

common.scrollUpDown($('.mui-scroll.one'),function(index){
    if(index>0&&isUp){
        isUp = false;
        getshop($('.search').val());
    }else if(index<0){
        $('.main').html('');
        isUp = true;
        page = 1;
        getshop($('.search').val());
        getchip();
    }else if(index>0 && !isUp){
        $('.mui-scroll').children('.ys-scroll-btm').html('已经到底了');
    }
});

//限时道具
function proptime(){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_card/usersTimeLimitCardsList",
        dataType:'jsonp',
        data: {uid:uid,token:token},
        success: function(data) {
            if(data.error_code == 0){
                var oproptime ='';
                for(var i=0;i<data.data.length; i++){
                    var otype =data.data[i].type;
                    var is_invalid;
                    var newtime=timemsg(data.data[i].get_time);
                    var newtime2=timemsg(data.data[i].expiry_time);
                    data.data[i].is_invalid==0 ? is_invalid='">未使用':is_invalid=' lose">已失效';
                    otype == 3 ? otype='补签卡': otype == 4? otype='好友卡':otype='约会卡';
                    oproptime+='<div class="card "><p class="card-title ">'+otype+'X'+data.data[i].num+'<span class="color '+is_invalid
                              +'</span></p><p class="card-text ">发放原因：'+data.data[i].source+'</p>'
                              +'<p class="card-text ">发放时间：'+newtime+'</p>'
                              +'<p class="card-text ">到期时间：'+newtime2+'</p></div>';
                }
                $('.mui-scroll-wrapper').eq(1).find('.mui-scroll ').append(oproptime);
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
            }
        }  
    });
}


//时间转换
function timemsg(time){
    var date = new Date(time * 1000);
    var year = date.getFullYear();
    var mou = date.getMonth() + 1;
    mou<10? mou='0'+mou :null;
    var day = date.getDate();
    day<10? day='0'+day :null;
    var hours = date.getHours();
    hours<10? hours='0'+hours :null;
    var minutes = date.getMinutes(); 
    minutes<10? minutes='0'+minutes :null;
    var newtime = year + '-' + mou + '-' + day + ' ' + hours + ':' +minutes ;
    return newtime;
}


//获取卡片
function getcard(){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_card/getCardsNoExpiry",
        dataType:'jsonp',
        data: {uid:uid,token:token},
        success: function(data) {
            if(data.error_code == 0){
                var ohtml='';
                for(var i=0 ; i<data.data.length ; i++){
                    data.data[i].type == 5 ? ohtml +='<p class="card-title yhk">我的约会卡<span >X'+data.data[i].num+'</span></p>': data.data[i].type == 4 ?ohtml +='<p class="card-title hyk">我的好友卡<span >X'+data.data[i].num+'</span></p>': ohtml +='<p class="card-title bqk">我的补签卡<span >X'+data.data[i].num+'</span></p>';
                }
                $('.mui-scroll-wrapper .cards').html(ohtml);
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
//              mui.toast(data.error_msg);
            }
        }  
    });
}

//获取礼物
function getgift(){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_gift/getMyGift",
        dataType:'jsonp',
        data: {uid:uid,token:token},
        success: function(data) {
            if(data.error_code == 0){
                var ohtml='';
                for(var i=0 ; i<data.data.length ; i++){
                    ohtml+='<a class="gift "><span class="giftimg-div "><img class="giftimg " src="'+DOMAIN_NAME+data.data[i].url+'"/></span><span class="text ">'+data.data[i].name+' ×'+data.data[i].sum+'</span><em>'+parseInt(data.data[i].money)+'块币</em></a>';
                }
                $('.mui-scroll-wrapper .con-gift-p').html(ohtml);
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
            }
        }  
    });
}



